﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using MarsExplorerServer.Models;

namespace MarsExplorerServer
{
    public class Service1 : IService1
    {
        Game teamGame;
        public Service1()
        {
           // Callback.message("PING");
        }
       
       
        public string addResource(int TeamID, int ResourceID,int value)
        {
            // TODO: Implement this method
            teamGame.addResource(TeamID,ResourceID, value);
            return "ok";
        }

        public Game getGame()
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }

        public Team getTeam(int TeamID)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }

        public Player getPlayer(int PlayerID)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }

        public string createGame()
        {
            teamGame = new Game();
            return "ok";
        }

        public string addTeam(Team value)
        {
            try
            {
                teamGame.addTeam(value);
                return "ok";
            }
            catch (Exception)
            {

                return "error";
            }
           
        }

        public string addPlayer(Team value)
        {
            try
            {
                teamGame.addPlayer(value.Players[0], value);
                return "ok";
            }
            catch (Exception)
            {

                return "error";
            }
           
        }

        public MarsExplorerServer.CompositeType ping2(int value)
        {
            Form1 currentForm = Application.OpenForms[0] as Form1;
            SendOrPostCallback callback = delegate { currentForm.LabelToUpdate.Text = DateTime.Now.ToString(); };
            CompositeType tt = new CompositeType { BoolValue = true, StringValue = "hello" };
            return tt;
        }

        public string ping(int value)
        {
            // TODO: Implement this method
            Form1 currentForm = Application.OpenForms[0] as Form1;
            SendOrPostCallback callback = delegate { currentForm.LabelToUpdate.Text = DateTime.Now.ToString(); };
           // Callback.Equals(OperationContext.Current.SessionId);
            //currentForm.formSyncContext.Send(callback, null);

            return "ping";
            //currentForm.formSyncContext.Send(callback2, null);
        }

        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }

        ICalculatorDuplexCallback Callback
        {
            get
            {
                return OperationContext.Current.GetCallbackChannel<ICalculatorDuplexCallback>();
            }
        }
    }
}
